package com.ywu.file.sys.standard.service;

import com.ywu.app.manage.spi.dto.YwuAppUserDto;
import com.ywu.app.manage.spi.entity.YwuAppEntity;
import com.ywu.app.manage.spi.spi.YwuAppSpi;
import com.ywu.boot.context.YwuContextHolder;
import com.ywu.common.entity.PageList;
import com.ywu.common.page.PageService;
import com.ywu.common.result.CommonResult;
import com.ywu.common.utils.TimerIdGenerateUtil;
import com.ywu.file.sys.standard.data.YwuFileAppPermissionRepository;
import com.ywu.file.sys.standard.dto.YwuFileAppPermissionDto;
import com.ywu.file.sys.standard.entity.YwuFileAppPermissionEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @ClassName YwuFileAppPermissionService
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/8/15 19:28
 * @Version 1.0
 **/
@Service
public class YwuFileAppPermissionService {
    @Autowired
    private YwuFileAppPermissionRepository repository;

    @Autowired
    private YwuAppSpi ywuAppSpi;

    /**
    * @Author GroundDemo
    * @Description 分页查询授权信息
    * @Date 19:38 2024/8/15
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryPageList(YwuFileAppPermissionDto dto) {
        dto.setSourceAppId(YwuContextHolder.getCurrentContext().getYwuContextApp().getAppId());
        // 查询数量
        int count = repository.queryPageCount(dto);
        // 构建分页参数
        PageService.execPageStartAndEnd(dto.getPageInfo());
        // 分页查询数据
        List<YwuFileAppPermissionEntity> entityList = repository.queryPageList(dto);
        // 关联应用名称
        Set<String> sourceAppIds = entityList.stream().map(YwuFileAppPermissionEntity::getSourceAppId).collect(Collectors.toSet());
        Set<String> permissionAppIds = entityList.stream().map(YwuFileAppPermissionEntity::getPermissionAppId).collect(Collectors.toSet());
        sourceAppIds.addAll(permissionAppIds);
        List<YwuAppUserDto> searchList = new ArrayList<>();
        for (String appId : sourceAppIds) {
            YwuAppUserDto ywuAppDto = new YwuAppUserDto();
            ywuAppDto.setAppId(appId);
            searchList.add(ywuAppDto);
        }
        CommonResult<List<YwuAppEntity>> result = ywuAppSpi.queryAppInfoByAppId(searchList);
        if (Objects.nonNull(result) && result.getCode().equals("200")) {
            List<YwuAppEntity> data = result.getData();
            Map<String, YwuAppEntity> collect = data.stream().collect(Collectors.toMap(YwuAppEntity::getAppId, Function.identity()));
            for (YwuFileAppPermissionEntity entity : entityList) {
                if (collect.containsKey(entity.getSourceAppId())) {
                    entity.setSourceAppName(collect.get(entity.getSourceAppId()).getAppName());
                }
                if (collect.containsKey(entity.getPermissionAppId())) {
                    entity.setPermissionAppName(collect.get(entity.getPermissionAppId()).getAppName());
                }
            }
        }
        // 构建返回分页对象
        PageList<YwuFileAppPermissionEntity> resPageList = new PageList<>(dto.getPageInfo().getPageIndex(),
                dto.getPageInfo().getPageSize(), count, entityList);
        return CommonResult.success(resPageList);
    }

    /**
    * @Author GroundDemo
    * @Description 添加授权映射
    * @Date 19:42 2024/8/15
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> addPermissionInfo(YwuFileAppPermissionDto dto) {
        // 判断是否存在授权关系
        YwuFileAppPermissionEntity search = new YwuFileAppPermissionEntity();
        search.setSourceAppId(YwuContextHolder.getCurrentContext().getYwuContextApp().getAppId());
        search.setPermissionAppId(dto.getPermissionAppId());
        YwuFileAppPermissionEntity entity = repository.getEntity(search);
        if (Objects.nonNull(entity)) {
            return CommonResult.error().setErrorCode("app.permission.is.exists")
                    .setMessage("授权关系已存在");
        }
        // 构建entity对象
        YwuFileAppPermissionEntity ywuFileAppPermissionEntity = new YwuFileAppPermissionEntity();
        ywuFileAppPermissionEntity.setPermissionAppId(dto.getPermissionAppId());
        ywuFileAppPermissionEntity.setSourceAppId(YwuContextHolder.getCurrentContext().getYwuContextApp().getAppId());
        ywuFileAppPermissionEntity.setId(TimerIdGenerateUtil.nextId());
        ywuFileAppPermissionEntity.createInfo(YwuContextHolder.getCurrentContext().getYwuContextUser().getUserAccount());
        repository.addPermissionInfo(ywuFileAppPermissionEntity);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 删除授权关系
    * @Date 19:47 2024/8/15
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> deletePermissionInfo(YwuFileAppPermissionDto dto) {
        YwuFileAppPermissionEntity ywuFileAppPermissionEntity = new YwuFileAppPermissionEntity();
        ywuFileAppPermissionEntity.setId(dto.getId());
        ywuFileAppPermissionEntity.setSourceAppId(YwuContextHolder.getCurrentContext().getYwuContextApp().getAppId());
        YwuFileAppPermissionEntity entity = repository.getEntity(ywuFileAppPermissionEntity);
        if (Objects.isNull(entity)) {
            return CommonResult.error().setErrorCode("app.permission.not.exists")
                    .setMessage("授权关系不存在存在");
        }
        repository.deletePermissionInfoById(ywuFileAppPermissionEntity);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 根据AppId查询授权列表
    * @Date 19:50 2024/8/15
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.util.List<com.ywu.file.sys.standard.entity.YwuFileAppPermissionEntity>>
    **/
    public CommonResult<List<YwuFileAppPermissionEntity>> getPermissionByAppId(YwuFileAppPermissionDto dto) {
        List<YwuFileAppPermissionEntity> entityList = repository.queryPageList(dto);
        if (CollectionUtils.isEmpty(entityList)) {
            return CommonResult.success(new ArrayList<>());
        }
        return CommonResult.success(entityList);
    }
}
